'use strict';

const Controller = require('egg').Controller;

class TopicController extends Controller {
    // 创建控制器——POST——/name
    async create() {
        const { ctx } = this;
        const { title, description, ins_id } = ctx.request.body;
        const pass = 0;
        const deleted = 0;
        const topic = {
            title,
            description,
            deleted,
            ins_id,
            pass
        }
        const result = await ctx.service.topic.add(topic);

        ctx.status = 201;
        ctx.body = result;
    }

    // // 查询获取数据用控制器——GET——/name?keywords=?&&.....
    async index() {
        const { ctx } = this;
        const keywords = ctx.query.keywords ? ctx.query.keywords : '';
        const pageIndex = ctx.query.pageIndex ? +ctx.query.pageIndex : 1;
        const pageSize = ctx.query.pageSize ? +ctx.query.pageSize : 5;
        console.log('keywords', keywords);
        const result = await ctx.service.topic.findList(keywords, pageIndex, pageSize);
        ctx.status = 200;
        ctx.body = result;
    }
    // // 根据id获取信息的控制器方法——GET——/name/:id
    async show() {
        const { ctx } = this;
        const id = ctx.params.id;
        const result = await ctx.service.topic.findById(id);
        ctx.status = 200;
        ctx.body = result;
    }
    // // 根据id修改对应的数据的控制器方法——PUT——/name/:id
    async update() {
        const { ctx } = this;
        const id = ctx.params.id;
        const {
            title,
            stu_id,
            description,
            pass,
        } = ctx.request.body;
        const result = await ctx.service.topic.modify({ 
            id, 
            title,
            stu_id,
            description,
            pass,
        });
        ctx.body = result;
    }
    // // // 根据id删除对应的数据的控制器方法——DELETE——/name/:id
    async destroy(){
        const { ctx } = this;
        const id = ctx.params.id;
        const result = await ctx.service.topic.remove(id);
        ctx.status = 200;
        ctx.body = result;
    }

    async findByStuId(){
        const { ctx } = this;
        const stu_id = ctx.params.id;
        const result = await ctx.service.topic.findByStudentId(stu_id);
        ctx.status = 200;
        ctx.body = result;
    }

    async findByNeedReview(){
        const { ctx } = this;
        const keywords = ctx.query.keywords ? ctx.query.keywords : '';
        const pageIndex = ctx.query.pageIndex ? +ctx.query.pageIndex : 1;
        const pageSize = ctx.query.pageSize ? +ctx.query.pageSize : 5;
        const result = await ctx.service.topic.findByneedReview(keywords, pageIndex, pageSize);
        ctx.status = 200;
        ctx.body = result;
    }

}

module.exports = TopicController;
